<?php
namespace common\components\phpexecel;

class PayLogisticCommonFunc
{
    /**
     *  @DESC 数据导
     *  @notice 解决了上面导出列数过多的问题
     *  @example
     *  $data = [1, "小明", "25"];
     *  $header = ["id", "姓名", "年龄"];
     *  Myhelpers::exportData($data, $header);
     *  @return void, Browser direct output
     */
    public static function exportData ($data, $header, $title = "simple", $filename = "data")
    {
        //require relation class files
    //    require_once  'PHPExcel.php'; //用于phpExcel
      //  require_once  'PHPExcel/Writer/Excel2007.php'; //用于输出excel文档
//         require_once  'PHPExcel/RichText.php';
        require_once  'PHPExcel/Worksheet.php';
        //print_r($data);return;
        if (!is_array ($data) || !is_array ($header)) return false;
        $filename = date('Y-m-d H:i:s');
        $objPHPExcel = new \PHPExcel();
        // Set properties
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
        $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
        $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
        $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
        $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
        // Add some data
        $objPHPExcel->setActiveSheetIndex(0);
        
        //全部居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //添加头部
        $hk = 0;
        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach ($header as $k => $v)
        {
            $colum = \PHPExcel_Cell::stringFromColumnIndex($hk);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum."1", $v);
            $hk += 1;
        }
        $column = 2;
        foreach($data as $key => $rows)  //行写入
        {
            $span = 0;
             foreach($rows as $keyName => $value) // 列写入
             {
                $j = \PHPExcel_Cell::stringFromColumnIndex($span);
                //print_r($rows);return;
                //print_r($column);return;
                //||$j=='G'||$j=='I'
                if ($j == 'B'||$j=='E'||$j=='G'){
                    if($value==""){
                        $objActSheet->setCellValue($j.$column, $value);      
                    }else{
                        if (substr($value,strrpos($value, '.')) == '.jpg'  || substr($value,strrpos($value, '.')) == '.jpeg'|| substr($value,strrpos($value, '.')) == '.JPEG'||substr($value,strrpos($value, '.')) == '.JPG') {
                            $img = imagecreatefromjpeg(\Yii::$app->params['imgPath0'].$value);
                        } else {
                            $img = imagecreatefrompng(\Yii::$app->params['imgPath0'].$value);
                        }
                        $objDrawing = new \PHPExcel_Worksheet_MemoryDrawing();
                        
                        /*设置图片所在单元格的格式*/
                        $objDrawing->setOffsetX(30);
                        $objDrawing->setOffsetY(30);
                        $objDrawing->setCoordinates($j.$column);
                        
                        $objDrawing->setImageResource($img);
                        
                        $objDrawing->setOffsetX(20);
                        
                        $objDrawing->setOffsetY(20);
                        
                        $objActSheet->getRowDimension($column)->setRowHeight(100);
                        
                        $objDrawing->setRenderingFunction(\PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);//渲染方法
                        
                        $objDrawing->setMimeType(\PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
                        
                        /*图片高度*/
                        $objDrawing->setHeight(100);
                        
                        $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
                        
                    }
    
                }else {
                    $objActSheet->setCellValue($j.$column, $value);                    
                }
                //是否换行
                $objActSheet->getStyle($j)->getAlignment()->setWrapText(true);
                $span++;
            }
            $column++;
        }
        //设置宽度
        $objActSheet->getColumnDimension('A')->setWidth(26);
        $objActSheet->getColumnDimension('B')->setWidth(20);
        $objActSheet->getColumnDimension('C')->setWidth(20);
        $objActSheet->getColumnDimension('D')->setWidth(24);
        $objActSheet->getColumnDimension('E')->setWidth(20);
        $objActSheet->getColumnDimension('F')->setWidth(20);
        $objActSheet->getColumnDimension('G')->setWidth(20);
        // Rename sheet
        $objPHPExcel->getActiveSheet()->setTitle($title);
        // Save Excel 2007 file
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
        ob_end_clean();
        ob_implicit_flush(1);
        header("Pragma:public");
        header("Content-Type:application/x-msexecl;name=\"{$title}{$filename}.xls\"");
        header("Content-Disposition:inline;filename=\"{$title}{$filename}.xls\"");
        $objWriter->save("php://output");
    }
}

